package com.ckzp.weixin.model;


import com.ckzp.core.id.UUIDHex;
import com.ckzp.core.json.Jmode;
import com.ckzp.core.lc.EncoderHandler;
import com.ckzp.weixin.AccessTokenTool;
import com.ckzp.weixin.WxTool;

import java.util.Date;

/**
 * Created by 51594 on 2022/9/22.
 */
public class JsApiObject {
    /**
     *
     */
    private String noncestr = "";
    /**
     *
     */
    private String jsapi_ticket = "";
    /**
     *
     */
    private String timestamp = "";
    /**
     *
     */
    private String url = "";
    /**
     *
     */
    private String sign_str = "";
    /**
     *
     */
    private String sign = "";
    /**
     *
     */
    private String appid = "";

    public static JsApiObject getInstance(String url) {
        return new JsApiObject(url);
    }

    public static JsApiObject n(String url) {
        return new JsApiObject(url);
    }

    public JsApiObject(String url) {
        this.url = url;
        this.setJsapi_ticket(AccessTokenTool.getJsapiTicket());
        this.setNoncestr(UUIDHex.getUUIDHex());
        this.setTimestamp(String.valueOf((new Date()).getTime() / 1000L));
        this.setSign(this.getSignString());
    }

    public String getNoncestr() {
        return this.noncestr;
    }

    public void setNoncestr(String noncestr) {
        this.noncestr = noncestr;
    }

    public String getJsapi_ticket() {
        return this.jsapi_ticket;
    }

    public void setJsapi_ticket(String jsapi_ticket) {
        this.jsapi_ticket = jsapi_ticket;
    }

    public String getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getUrl() {
        return this.url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getSign() {
        return this.sign;
    }

    public String getAppid() {
        return WxTool.getAppID();
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String toString() {
        return "JsapiObject{noncestr=\'" + this.noncestr + '\'' + ", jsapi_ticket=\'" + this.jsapi_ticket + '\'' + ", timestamp=\'" + this.timestamp + '\'' + ", url=\'" + this.url + '\'' + ", sign_str=\'" + this.sign_str + '\'' + ", sign=\'" + this.sign + '\'' + '}';
    }

    /**
     * @return
     */
    private String getSignString() {
        StringBuffer sb = new StringBuffer();
        sb.append("jsapi_ticket=" + this.getJsapi_ticket());
        sb.append("&noncestr=" + this.getNoncestr());
        sb.append("&timestamp=" + this.getTimestamp());
        sb.append("&url=" + this.getUrl());
        this.sign_str = sb.toString();
        return EncoderHandler.getSHA1(sb.toString());
    }

    /**
     * @param urls
     * @return
     */
    public static Jmode getWxApi(String urls) {
        JsApiObject jo = n(urls);
        Jmode api = Jmode.n().set("appid", jo.getAppid()).set("timestamp", jo.getTimestamp()).set("noncestr", jo.getNoncestr()).set("signature", jo.getSign());
        return api;
    }

    /**
     * @param arg
     */
    public static void main(String[] arg) {
        String str1 = "jsapi_ticket=HoagFKDcsGMVCIY2vOjf9iaWkBUouvVoLpdsobVFlzui6F-PDaVrJy_VH47iUAlUVGx0GISQG99rdRyTP9svsw&noncestr=8a985eed5dcf971a015dcf9799f90001&timestamp=1502426405370&url=http://wx.ck-zp.com/yxd/test/accesstoken.jsp";
        String str = "jsapi_ticket=HoagFKDcsGMVCIY2vOjf9iaWkBUouvVoLpdsobVFlzui6F-PDaVrJy_VH47iUAlUVGx0GISQG99rdRyTP9svsw&noncestr=8a985eed5dcf971a015dcf99501a0008&timestamp=1502426517&url=http://wx.ck-zp.com/yxd/test/accesstoken.jsp";
        System.out.println(EncoderHandler.getSHA1(str));
    }
}
